const path = require('path');

module.exports = () => (req, res, next) => {
  if (req.path === '/auth/ping/callback') {
    const options = {
      root: path.join(__dirname, '../modules/passport-cirrus-ping/lib/'),
      headers: {
        'Content-Type': 'text/html',
      },
    };
    return res.sendFile('authClientSide.html', options);
  }
  const paths = ['/auth/ping', '/auth/token', '/auth/callback'];
  if (!paths.includes(req.path)) {
    if (!req.isAuthenticated || !req.isAuthenticated()) {
      req.session.returnTo = req.originalUrl;
      return res.redirect('/auth/ping');
    }
  }
  return next();
};
